<template>
  <div class="menu-page">
    <div class="page-header">
      <div class="container">
        <h1 class="page-title">{{ tableId }}号桌 - 菜品选择</h1>
        <p class="page-subtitle">选择您喜欢的菜品</p>
      </div>
    </div>

    <div class="container">
      <div class="menu-content">
        <!-- 分类导航 -->
        <div class="category-nav">
          <el-button
            v-for="category in categories"
            :key="category.id"
            :type="currentCategory === category.id ? 'primary' : 'default'"
            @click="currentCategory = category.id"
            class="category-btn"
          >
            <el-icon><component :is="category.icon" /></el-icon>
            {{ category.name }}
          </el-button>
        </div>

        <!-- 菜品列表 -->
        <div class="dishes-grid">
          <div
            v-for="dish in filteredDishes"
            :key="dish.id"
            class="dish-card"
            @click="showDishDetail(dish)"
          >
            <div class="dish-image">
              <img :src="dish.image" :alt="dish.name">
              <div class="dish-badges">
                <el-tag v-if="dish.spicy" type="danger" size="small">辣</el-tag>
                <el-tag v-if="dish.popular" type="warning" size="small">推荐</el-tag>
              </div>
            </div>
            <div class="dish-info">
              <h3 class="dish-name">{{ dish.name }}</h3>
              <p class="dish-description">{{ dish.description }}</p>
              <div class="dish-footer">
                <span class="dish-price">¥{{ dish.price }}</span>
                <div class="dish-actions">
                  <el-button
                    v-if="cartStore.getItemQuantity(dish.id) > 0"
                    type="danger"
                    size="small"
                    circle
                    @click.stop="removeFromCart(dish.id)"
                  >
                    <el-icon><Minus /></el-icon>
                  </el-button>
                  <span v-if="cartStore.getItemQuantity(dish.id) > 0" class="quantity">
                    {{ cartStore.getItemQuantity(dish.id) }}
                  </span>
                  <el-button
                    type="primary"
                    size="small"
                    circle
                    @click.stop="addToCart(dish)"
                  >
                    <el-icon><Plus /></el-icon>
                  </el-button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 购物车悬浮按钮 -->
    <div class="cart-fab" v-if="!cartStore.isEmpty" @click="goToCart">
      <el-badge :value="cartStore.totalItems" class="cart-badge">
        <el-button type="primary" size="large" circle>
          <el-icon><ShoppingCart /></el-icon>
        </el-button>
      </el-badge>
    </div>

    <!-- 菜品详情弹窗 -->
    <el-dialog v-model="showDetail" :title="selectedDish?.name" width="90%">
      <div v-if="selectedDish" class="dish-detail">
        <img :src="selectedDish.image" :alt="selectedDish.name" class="detail-image">
        <div class="detail-info">
          <p class="detail-description">{{ selectedDish.description }}</p>
          <div class="detail-price">¥{{ selectedDish.price }}</div>
          <div class="detail-actions">
            <el-button
              v-if="cartStore.getItemQuantity(selectedDish.id) > 0"
              type="danger"
              size="large"
              @click="removeFromCart(selectedDish.id)"
            >
              <el-icon><Minus /></el-icon>
              减少
            </el-button>
            <span v-if="cartStore.getItemQuantity(selectedDish.id) > 0" class="detail-quantity">
              {{ cartStore.getItemQuantity(selectedDish.id) }}
            </span>
            <el-button
              type="primary"
              size="large"
              @click="addToCart(selectedDish)"
            >
              <el-icon><Plus /></el-icon>
              添加
            </el-button>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useCartStore } from '../stores/cart'
import { dishes, categories, getDishesByCategory } from '../data/dishes'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()
const cartStore = useCartStore()

const tableId = computed(() => route.params.tableId)
const currentCategory = ref('all')
const showDetail = ref(false)
const selectedDish = ref(null)

const filteredDishes = computed(() => {
  return getDishesByCategory(currentCategory.value)
})

onMounted(() => {
  cartStore.setTableId(tableId.value)
})

const showDishDetail = (dish) => {
  selectedDish.value = dish
  showDetail.value = true
}

const addToCart = (dish) => {
  cartStore.addItem(dish)
  ElMessage.success(`已添加 ${dish.name}`)
}

const removeFromCart = (dishId) => {
  cartStore.updateQuantity(dishId, cartStore.getItemQuantity(dishId) - 1)
  ElMessage.info('已从购物车移除')
}

const goToCart = () => {
  router.push(`/cart/${tableId.value}`)
}
</script>

<style scoped>
.menu-page {
  min-height: 100vh;
  background: #f8f9fa;
}

.menu-content {
  padding: 40px 0;
}

.category-nav {
  display: flex;
  gap: 12px;
  margin-bottom: 32px;
  flex-wrap: wrap;
}

.category-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
}

.dishes-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 24px;
}

.dish-card {
  background: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  cursor: pointer;
}

.dish-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.dish-image {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.dish-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.dish-badges {
  position: absolute;
  top: 12px;
  right: 12px;
  display: flex;
  gap: 8px;
}

.dish-info {
  padding: 20px;
}

.dish-name {
  font-size: 18px;
  color: #333;
  margin: 0 0 8px 0;
}

.dish-description {
  color: #666;
  font-size: 14px;
  line-height: 1.5;
  margin: 0 0 16px 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.dish-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dish-price {
  font-size: 20px;
  font-weight: bold;
  color: #e6a23c;
}

.dish-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.quantity {
  min-width: 24px;
  text-align: center;
  font-weight: bold;
  color: #333;
}

.cart-fab {
  position: fixed;
  bottom: 30px;
  right: 30px;
  z-index: 1000;
}

.cart-badge {
  .el-badge__content {
    background-color: #f56c6c;
  }
}

.dish-detail {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.detail-image {
  width: 100%;
  max-height: 300px;
  object-fit: cover;
  border-radius: 8px;
}

.detail-info {
  text-align: center;
}

.detail-description {
  color: #666;
  line-height: 1.6;
  margin-bottom: 20px;
}

.detail-price {
  font-size: 28px;
  font-weight: bold;
  color: #e6a23c;
  margin-bottom: 20px;
}

.detail-actions {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
}

.detail-quantity {
  font-size: 20px;
  font-weight: bold;
  color: #333;
  min-width: 40px;
}

@media (max-width: 768px) {
  .category-nav {
    justify-content: center;
  }
  
  .dishes-grid {
    grid-template-columns: 1fr;
  }
  
  .cart-fab {
    bottom: 20px;
    right: 20px;
  }
}
</style> 